<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Dashboard;
use App\Models\User;
use App\Services\Wechat;
use App\Services\Msgtpl;
use Maatwebsite\Excel\Facades\Excel;
use App\Exports\DashboardExport;
use App\Repos\DashboardRepo;

class DashboardController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $pageNum = 0;
        $num     = 30;
        if($request->page>1) {
            $pageNum = ($request->page-1)*$num;
        }
        $param = [];
        $query = Dashboard::query();
        if($request->keyword) {
            $query->where('customer_name','like','%'.$request->keyword.'%')
                   ->orWhere('car_num','like','%'.$request->keyword.'%');
            $param['keyword'] = trim($request->keyword);        
        }
        $list = $query->orderBy('level','desc')
                      ->orderBy('id','desc')
                      ->paginate($num);
        $levelArr   = DashboardRepo::getLevelArr();
        $workers    = User::where('role_code',2)->get();
        return view('admin.dashboard.index',compact('list','param','levelArr','workers','pageNum'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        $workers    = User::where('role_code',2)->get();
        $levelArr   = DashboardRepo::getLevelArr();
        return view('admin.dashboard.add',compact('data','workers','levelArr'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rs['status'] = 'success';
        $rs['msg'] = '操作成功';
        $data = $request->except('_token');
        if(!$request->is_push) {
            $data['worker_id'] = '';
        }
        $rs   = Dashboard::create($data);
        if($request->is_push==1) {
            
            $worker = User::find($request->worker_id);
          
            $msg = $data;          
            $msg['name']        = $data['customer_name'];
            $msg['arrived_at']  = $data['in_date'];
            $msg['first']       = '进场维修通知';   
            $msg['worker_name'] = $worker->truename; 
            $msg['phone']  = $data['mobile'];
           
            $msg['url']    = config('app.url').'/user/dashboard/show/'.$rs->id;
           
            // 推送消息
            $worker        = User::find($request->worker_id); 
            $msg['openid'] = $worker->openid;       
            Wechat::sendMsgTpl('new_to_client',$msg);
            return redirect('zadmin/dashboard');
        }
        return redirect('zadmin/dashboard')->with('rs',$rs);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data       = Dashboard::find($id);
        $levelArr   = DashboardRepo::getLevelArr();
       
        return view('admin.dashboard.edit',compact('data','levelArr'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token','_method','preUrl');
        if($request->preUrl) {
            $preUrl = $request->preUrl;
        }else{
            $preUrl = 'zadmin/dashboard';
        }

        $rs   = Dashboard::where('id',$id)->update($data);
        if($rs) {
            return redirect($preUrl);
        }
        return back()->withInput();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $rs = Dashboard::destroy($id);
        if ($rs) {
            return redirect('zadmin/dashboard');
        }
        return back();
    }

    //导出工单。
    public function export(Request $request)
    {
        $param = [];
        if($request->keyword) {
            $param['keyword'] = (int)$request->keyword;
        }
        return Excel::download(new DashboardExport($param), '车辆维修看板'.date('YmdHi').'.xlsx');   
    }


    /**
     * 派工
     */
    public function process(Request $request)
    {
        
        $rs['status'] = 'danger';
        $rs['msg']    = '操作成功';
        $data = $request->except(['_token']);
        //dd($data);
        $flag = DashboardRepo::dispatch($data);
        if($flag == true) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return back()->with('rs',$rs);
        } 
        return back()->with('rs',$rs);
        
    }
}
